<?php
define('INSIDE', true);
define('SITE_ROOT', __DIR__.'/../..');

require_once __DIR__.'/controller.php';
require_once __DIR__.'/util.php';
require_once __DIR__.'/db.php';
require_once __DIR__.'/language.php';
require_once __DIR__.'/template.php';

class site_core{
	private static $_instance = null;
	
	private $_env = null;
	// system configs, dont modify them!
	private $_config = array();
	private $_db = null;
	private $_user = array();
	// all the vars which related to page needed
	private $_var = array();
	
	public $init_filesystem = true;
	public $init_db = true;
	
	private static function &_instance(){
		if(!(self::$_instance instanceof self)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	private function __construct() {
		$this->_init_config();
		$this->_init_app();
		$this->_init_filesystem();
		$this->_init_session();
	}
	
	public function init() {
		$this->init_db();
	}
	
	//__clone方法防止对象被复制克隆
	private function __clone(){
		trigger_error('Clone is not allow!',E_USER_ERROR);
	}
	
	private function _init_app(){
		global $_GVAR;
		$_GVAR['path']['cache']['tpl']=$this->_config['path']['tplcache'];
		$_GVAR['path']['assets']=$this->_config['path']['assets'];
		
		// theme
		$_GVAR['theme']=$this->_config['theme']['default_theme'];
		
		// language
		$_GVAR['language'] = $this->_config['language'];
		$_GVAR['langtext'] = array();
		
		$this->_var = & $_GVAR;
	}
	
	private function _init_config(){
		$_config = null;
		require SITE_ROOT.'/protected/config/main.php';
		if(empty($_config)){
			echo 'Missing config file.';
			exit;
		}
		$this->_config = &$_config;
	}
	
	private function _init_filesystem(){
		if($this->init_filesystem) {
			global $_GVAR;
			$file = SITE_ROOT.'/'.$this->_config['path']['tplcache'];
			if(! file_exists($file)) {
				mkdir($file, 0, true);
			}
			
			$file = SITE_ROOT.'/'.$this->_config['path']['assets'];
			if(! file_exists($file)) {
				mkdir($file, 0, true);
			}
		}
	}
	
	private function _init_session() {
		session_start();
	}
	
	private function init_db(){
		if($this->init_db) {
			$db_class = 'db_'.$this->_config['db']['type'];
			$this->_db = DB::instance($db_class, $this->_config['db']);
			$this->_db->connect();
		}
	}
	
	public static function &instance(){
		return self::_instance();
	}
	
	public static function reinit(){
		self::free();
		self::instance();
	}
	
	public static function free(){
		self::instance()->_db->close();
		self::$_instance = null;
	}
	
	public static function &ENV(){
		return self::instance()->_env;
	}
	
	public static function &CFG(){
		return self::instance()->_config;
	}
	
	public static function &DB(){
		return self::instance()->_db;
	}
	
	public static function &USER(){
		return self::instance()->_user;
	}
	
	public static function &GVAR(){
		return self::instance()->_var;
	}
}

class DB{
	private static $_instance = null;
	
	public static function &instance($db_class = 'db_mysql', $db_cfg = null){
		if(!(self::$_instance instanceof self)){
			self::$_instance = new $db_class();
			self::$_instance->set_cfg($db_cfg);
		}
		return self::$_instance;
	}
	
	public static function connect(){
		self::instance()->connect();
	}
	public static function close(){
		self::instance()->close();
	}
	public static function query($query){
		return self::instance()->query($query);
	}
	public static function result($result, $row = 0){
		return self::instance()->result($result, $row);
	}
	public static function num_rows($result){
		return self::instance()->num_rows($result);
	}
	public static function num_fields($result){
		return self::instance()->num_fields($result);
	}
	public static function free_result($result){
		return self::instance()->free_result($result);
	}
	public static function fetch_row($result){
		return self::instance()->fetch_row($result);
	}
	public static function fetch_assoc($result){
		return self::instance()->fetch_assoc($result);
	}
	public static function fetch_fields($result){
		return self::instance()->fetch_fields($result);
	}
}

// inititalization
site_core::instance();

class C {
	public static function app() {
		return site_core::instance();
	}
	
	public static function theme_name() {
		$gvar = self::app()->GVAR();
		
		return $gvar['theme'];
	}
	
	public static function curr_user() {
		if(! isset($_SESSION['curr_user'])) {
			return false;
		}
		$user = $_SESSION['curr_user'];
		if(empty($user)) {
			unset($_SESSION['curr_user']);
			return false;
		}
		else {
			return $user;
		}
	}
}
?>